package com.sskj.gcc.presenter;

import android.widget.Toast;

import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.gcc.http.HttpConfig;
import com.sskj.gcc.ui.activity.RegisterActivity;
import com.sskj.lib.http.JsonCallBack;


public class RegisterActivityPresenter extends BasePresenter<RegisterActivity> {

    public void sendCode(String account, String mcode) {
        if (account.contains("@")) {
            sendEmail(account, "register");
        } else {
            sendSms(account, mcode, "register");
        }
    }

    /**
     * 发送验证码（注册）
     *
     * @param mobile 手机号
     * @param type   1注册 （2 重置 3 安全验证 4 资金密码设置 5 提币）
     * @return
     */
    public void sendSms(String mobile, String mcode, String type) {

        httpService.sendSms(mobile, mcode, type).execute(callback);
    }

    JsonCallBack<HttpData> callback = new JsonCallBack<HttpData>(this) {
        @Override
        public void onSuccess(Response<HttpData> response) {
            HttpData httpData = response.body();
            ToastUtil.showShort(httpData.getMsg());
            if (httpData.getCode() == HttpConfig.OK) {
                mView.sendCheckCodeSuccess();
            } else {
                ToastUtil.showShort(httpData.getMsg());
                mView.sendCheckCodeFail();
            }
        }

        @Override
        public void onError(Response<HttpData> response) {
            mView.sendCheckCodeFail();
            super.onError(response);
        }
    };

    /**
     * 发送邮件
     *
     * @param email 邮箱号
     * @return
     */
    public void sendEmail(String email, String type) {
        httpService.sendEmail(email, type).execute(callback);
    }


    public void checkCode(String account, String code) {

        mView.checkCodeSuccess();

//        httpService.checkCode(account, code)
//                .execute(new JsonCallBack<HttpData>() {
//                    @Override
//                    public void onSuccess(Response<HttpData> response) {
//                        HttpData httpData = response.body();
//                        if (httpData.getCode() == HttpConfig.OK) {
//                            mView.checkCodeSuccess();
//                        }else {
//                            ToastUtil.showShort(httpData.getMsg());
//                        }
//                    }
//                });
    }

    /**
     * 注册
     *
     * @param email  邮箱号
     * @param phone  手机号
     * @param code   验证码
     * @param opwd   密码
     * @param opwd1  重复密码
     * @param tjuser 推荐码/邀请码
     * @return
     */
    public void register(String email, String phone, String mcode, String code, String opwd, String opwd1, String tjuser) {
        httpService.register(email, phone, code, opwd, opwd1, tjuser).execute(new JsonCallBack<HttpData>(this) {
            @Override
            public void onSuccess(Response<HttpData> response) {
                HttpData httpData = response.body();
                ToastUtil.showShort(httpData.getMsg());
                if (httpData.getCode() == HttpConfig.OK) {
                    mView.registerSuccess();
                }
            }
        });
    }
}
